//
//  NBSellViewController.m
//  qiFenQi
//
//  Created by 戒了。 on 16/4/14.
//  Copyright © 2016年 xiaomage. All rights reserved.
//

#import "NBSellViewController.h"
#import "NBTitleButton.h"
#import "NBAllViewController.h"
#import "NBPhoneViewController.h"
#import "NBComputerViewController.h"
#import "NB3CViewController.h"
#import "NBShootViewController.h"
#import "NBWatchViewController.h"

@interface NBSellViewController ()

@property (nonatomic, weak) UIScrollView *titleScrollView;
@property (nonatomic, weak) UIScrollView *contentScrollView;
/** 上一次点击的标题按钮 */
@property (nonatomic, weak) NBTitleButton *previousClickedTitleButton;
@end

@implementation NBSellViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //导航条中间标题
    self.navigationItem.title = @"今日特卖";
    
    //初始化子控制器
    [self setupChildVcs];
    
    // 1.添加标题滚动视图
    [self setupTitleScrollView];
    
    // 2.添加内容滚动视图
    [self setupContentScrollView];
    
}

/**
 *  初始化子控制器
 */
- (void)setupChildVcs
{
    [self addChildViewController:[[NBAllViewController alloc] init]];
    [self addChildViewController:[[NBPhoneViewController alloc] init]];
    [self addChildViewController:[[NBComputerViewController alloc] init]];
    [self addChildViewController:[[NB3CViewController alloc] init]];
    [self addChildViewController:[[NBShootViewController alloc] init]];
    [self addChildViewController:[[NBWatchViewController alloc] init]];
}

#pragma mark - 添加标题滚动视图
- (void)setupTitleScrollView
{
    UIScrollView *titleScrollView = [[UIScrollView alloc] init];
    CGFloat y = self.navigationController.navigationBarHidden ? 20 : 64;
    titleScrollView.frame = CGRectMake(0, y, NBScreenW, 44);
    titleScrollView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:titleScrollView];
    _titleScrollView = titleScrollView;
    //标题滚动范围
    titleScrollView.contentSize = CGSizeMake(NBScreenW, 0);
    
    //设置标题按钮
    [self setupTitleButtons];
    
}

#pragma mark - 添加内容滚动视图
- (void)setupContentScrollView
{
    // 不要去自动调整scrollView的内边距
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    UIScrollView *contentScrollView = [[UIScrollView alloc] init];
    CGFloat y = CGRectGetMaxY(_titleScrollView.frame);
    contentScrollView.frame = CGRectMake(0, y, NBScreenW, NBScreenH - y);
    contentScrollView.backgroundColor = [UIColor blueColor];
    contentScrollView.showsHorizontalScrollIndicator = NO;
    contentScrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:contentScrollView];
    _contentScrollView = contentScrollView;
    //滚动内容范围
    contentScrollView.contentSize = CGSizeMake(NBScreenW, 0);
    
    //添加5个模块
    for (NSInteger i = 0; i < 6; i++) {
        UIView *childVcView = self.childViewControllers[i].view;
        childVcView.frame = CGRectMake(i * contentScrollView.nb_width, 0, contentScrollView.nb_width, contentScrollView.nb_height);
        //        childVcView.frame = CGRectMake(i * scrollView.xmg_width, 99, scrollView.xmg_width, scrollView.xmg_height - 99 - 49);
        [contentScrollView addSubview:childVcView];
    }
    
    
}

//设置标题按钮
- (void)setupTitleButtons
{
    NSArray *titles = @[@"全部",@"手机",@"电脑",@"3C",@"摄影",@"手表"];
    NSInteger count = titles.count;
    CGFloat titleButtonH = self.titleScrollView.nb_height - 10;
    CGFloat titleButtonW = self.titleScrollView.nb_width / count;
    
    for (NSInteger i = 0; i < count; i++) {
        NBTitleButton *titleButton = [[NBTitleButton alloc] init];
        [titleButton setTitle:titles[i] forState:UIControlStateNormal];
        titleButton.frame = CGRectMake(i * titleButtonW, 0, titleButtonW, titleButtonH);
        [titleButton addTarget:self action:@selector(titleButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.titleScrollView addSubview:titleButton];
        
    }
    
}


#pragma mark - 点击了标题按钮
- (void)titleButtonClick:(NBTitleButton *)titleButton
{
    // 修改按钮的状态
    // 上一个点击的按钮 -> 暗灰色
    self.previousClickedTitleButton.selected = NO;
    // 新点击的按钮 -> 红色
    titleButton.selected = YES;
    self.previousClickedTitleButton = titleButton;
    NBLog(@"点击了标题按钮");
}
@end
